home *** CD-ROM | disk | FTP | other *** search
/ Scene 96 / Scene 96 International Edition (Zyklop Software) (Disc 2) (1997).iso / misc / coding / midas060 / samples / api / all.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-17  |  6.9 KB  |  239 lines

  1. /*      all.c
  2.  *
  3.  * A great all-in-one DLL API example
  4.  *
  5.  * Copyright 1996,1997 Housemarque Inc.
  6.  *
  7.  * This file is part of the MIDAS Sound System, and may only be
  8.  * used, modified and distributed under the terms of the MIDAS
  9.  * Sound System license, LICENSE.TXT. By continuing to use,
  10.  * modify or distribute this file you indicate that you have
  11.  * read the license and understand and accept it fully.
  12. */
  13.  
  14. #if defined(__NT__) || defined(__WINDOWS__) || defined(_MSC_VER)
  15. #define WIN32_LEAN_AND_MEAN
  16. #include <windows.h>
  17. #endif
  18.  
  19. #include <stdio.h>
  20. #include <conio.h>
  21. #include <stdlib.h>
  22. #include "midasdll.h"
  23.  
  24. /* No stream support in DOS: */
  25. #ifdef __DOS__
  26. #define NOSTREAMS
  27. #endif
  28.  
  29.  
  30. /* We'll use a maximum of 2 stream channels, 4 sample channels and
  31.    16 channels for music: */
  32. #define NUMSTREAMCHANNELS 2
  33. #define NUMSAMPLECHANNELS 4
  34. #define NUMMUSICCHANNELS 16
  35.  
  36.  
  37. /****************************************************************************\
  38. *
  39. * Function:     void MIDASerror(void)
  40. *
  41. * Description:  Handles a MIDAS error - displays an error message and exits
  42. *
  43. \****************************************************************************/
  44.  
  45. void MIDASerror(void)
  46. {
  47.     int         error;
  48.  
  49.     error = MIDASgetLastError();
  50.     printf("\nMIDAS error: %s\n", MIDASgetErrorMessage(error));
  51.     if ( !MIDASclose() )
  52.     {
  53.         printf("\nBIG PANIC! MIDASclose Failed: %s\n", MIDASgetErrorMessage(
  54.             MIDASgetLastError()));
  55.     }
  56.     exit(EXIT_FAILURE);
  57. }
  58.  
  59.  
  60.  
  61. static MIDASmodule module;
  62. static MIDASstreamHandle stream1, stream2;
  63. static MIDASsample sample1, sample2;
  64. static MIDASsamplePlayHandle playHandle1, playHandle2;
  65.  
  66.  
  67. int main(void)
  68. {
  69.     int         key, exit = 0;
  70.  
  71.     MIDASstartup();
  72.  
  73.     setbuf(stdout, NULL);
  74.  
  75.     /* Flag that we don't have a module, effects or streams playing: */
  76.     module = NULL;
  77.     stream1 = stream2 = NULL;
  78.     sample1 = sample2 = 0;
  79.     playHandle1 = playHandle2 = 0;
  80.  
  81.     /* Decrease the size of buffer used: */
  82.     MIDASsetOption(MIDAS_OPTION_MIXBUFLEN, 150);
  83.     MIDASsetOption(MIDAS_OPTION_MIXBUFBLOCKS, 4);
  84.  
  85.     /* Initialize MIDAS and start background playback (at 100 polls
  86.        per second): */
  87.     if ( !MIDASinit() )
  88.         MIDASerror();
  89.     if ( !MIDASstartBackgroundPlay(100) )
  90.         MIDASerror();
  91.  
  92.     /* Open all channels: */
  93.     if ( !MIDASopenChannels(NUMSTREAMCHANNELS + NUMSAMPLECHANNELS +
  94.         NUMMUSICCHANNELS) )
  95.         MIDASerror();
  96.  
  97.     /* The first NUMSTREAMCHANNELS channels are used for streams, the next
  98.        NUMSAMPLECHANNELS for samples and the rest for music */
  99.  
  100.     /* Set automatic sample channel range: */
  101.     if ( !MIDASsetAutoEffectChannels(NUMSTREAMCHANNELS, NUMSAMPLECHANNELS) )
  102.         MIDASerror();
  103.  
  104.     /* Load our samples: */
  105.     if ( (sample1 = MIDASloadRawSample("..\\data\\explosi1.pcm",
  106.         MIDAS_SAMPLE_8BIT_MONO, MIDAS_LOOP_NO)) == 0 )
  107.         MIDASerror();
  108.     if ( (sample2 = MIDASloadRawSample("..\\data\\laugh1.pcm",
  109.         MIDAS_SAMPLE_8BIT_MONO, MIDAS_LOOP_YES)) == 0 )
  110.         MIDASerror();
  111.  
  112.     /* Loop, reading user input, until we should exit: */
  113.     while ( !exit )
  114.     {
  115.         puts("Keys:     1/2     Play/Stop sample 1\n"
  116.              "          q/w     Play/Stop sample 2\n"
  117. #ifndef NOSTREAMS
  118.              "          3/4     Play/Stop stream 1\n"
  119.              "          e/r     Play/Stop stream 2\n"
  120. #endif
  121.              "          5/6     Play/Stop module\n"
  122.              "          Esc     Exit\n");
  123.  
  124.         key = getch();
  125.  
  126.         switch ( key )
  127.         {
  128.             case 27:
  129.                 exit = 1;
  130.                 break;
  131.  
  132.             case '1':
  133.                 if ( (playHandle1 = MIDASplaySample(sample1,
  134.                     MIDAS_CHANNEL_AUTO, 0, 22050, 64, MIDAS_PAN_MIDDLE)) == 0)
  135.                     MIDASerror();
  136.                 break;
  137.  
  138.             case '2':
  139.                 if ( playHandle1 != 0 )
  140.                 {
  141.                     if ( !MIDASstopSample(playHandle1) )
  142.                         MIDASerror();
  143.                 }
  144.                 break;
  145.  
  146.             case 'q':
  147.                 if ( (playHandle2 = MIDASplaySample(sample2,
  148.                     MIDAS_CHANNEL_AUTO, 0, 16000, 64, -20)) == 0 )
  149.                     MIDASerror();
  150.                 break;
  151.  
  152.             case 'w':
  153.                 if ( playHandle2 != 0 )
  154.                 {
  155.                     if ( !MIDASstopSample(playHandle2) )
  156.                         MIDASerror();
  157.                 }
  158.                 break;
  159.  
  160. #ifndef NOSTREAMS
  161.             case '3':
  162.                 if ( stream1 != NULL )
  163.                 {
  164.                     if ( !MIDASstopStream(stream1) )
  165.                         MIDASerror();
  166.                 }
  167.                 if ( (stream1 = MIDASplayStreamFile(0, "e:\\fable-mono.sw",
  168.                     MIDAS_SAMPLE_16BIT_MONO, 44100, 500, 0)) == NULL )
  169.                     MIDASerror();
  170.                 break;
  171.  
  172.             case '4':
  173.                 if ( stream1 != NULL )
  174.                 {
  175.                     if ( !MIDASstopStream(stream1) )
  176.                         MIDASerror();
  177.                     stream1 = NULL;
  178.                 }
  179.                 break;
  180.  
  181.             case 'e':
  182.                 if ( stream2 != NULL )
  183.                 {
  184.                     if ( !MIDASstopStream(stream2) )
  185.                         MIDASerror();
  186.                 }
  187.                 if ( (stream2 = MIDASplayStreamFile(1,
  188.                     "..\\data\\powerups.pcm", MIDAS_SAMPLE_8BIT_MONO, 11025,
  189.                     500, 1)) == NULL )
  190.                     MIDASerror();
  191.                 break;
  192.  
  193.             case 'r':
  194.                 if ( stream2 != NULL )
  195.                 {
  196.                     if ( !MIDASstopStream(stream2) )
  197.                         MIDASerror();
  198.                     stream2 = NULL;
  199.                 }
  200.                 break;
  201. #endif /* #ifndef NOSTREAMS */
  202.  
  203.             case '5':
  204.                 if ( module != NULL )
  205.                 {
  206.                     if ( !MIDASstopModule(module) )
  207.                         MIDASerror();
  208.                     if ( !MIDASfreeModule(module) )
  209.                         MIDASerror();
  210.                 }
  211.                 if ( (module = MIDASloadModule("..\\data\\templsun.xm"))
  212.                     == NULL )
  213.                     MIDASerror();
  214.                 if ( !MIDASplayModule(module, 0) )
  215.                     MIDASerror();
  216.                 break;
  217.  
  218.             case '6':
  219.                 if ( module != NULL )
  220.                 {
  221.                     if ( !MIDASstopModule(module) )
  222.                         MIDASerror();
  223.                     if ( !MIDASfreeModule(module) )
  224.                         MIDASerror();
  225.  
  226.                     module = NULL;
  227.                 }
  228.                 break;
  229.         }
  230.     }
  231.  
  232.     /* Stop MIDAS: */
  233.     if ( !MIDASstopBackgroundPlay() )
  234.         MIDASerror();
  235.     if ( !MIDASclose() )
  236.         MIDASerror();
  237.  
  238.     return 0;
  239. }